package com.baizhi.cmfz.config;

import com.baizhi.cmfz.realm.AuthenRealm;
import com.baizhi.cmfz.realm.AuthorRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
    /*
     * 过滤器工厂设置
     *
    * */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
        //设置过滤器规格
        Map map=new HashMap();
        map.put("/login.jsp","anon");
        map.put("/admin/*","anon");
        map.put("/menu/*","authc");
        map.put("/guru/*","authc");

        //设置一个安全管理器
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        return  shiroFilterFactoryBean;
    }
    /* 创建安全管理器
    *   @Bean 如果工厂中有realm对象 自动装配
    * */

    @Bean
    public DefaultWebSecurityManager getSecurityManager(AuthorRealm authorRealm){
        DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
//        设置realm
        defaultSecurityManager.setRealm(authorRealm);
        return defaultSecurityManager;
    }

    @Bean
    public AuthorRealm getRealm(){
        return  new AuthorRealm();
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}

